//
//  ViewController.m
//  PCAForWar
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "CellForYaoWen.h"
#import "CellForChinaJunqing.h"
#import "VCForMySelf.h"
#import "ModelForScroll.h"
#import "YaoWenCell.h"
#import "ModelForYaoWen.h"
#import "VCForCell.h"
#import "ModelForChinaJunQIng.h"
#import <DKNightVersion.h>
#import <AFNetworking.h>
#import "QuestionVC.h"

#define WIDTH  self.view.frame.size.width
#define HEIGHT  self.view.frame.size.height

@interface ViewController () <UICollectionViewDataSource,UICollectionViewDelegate,NSURLSessionDelegate,NSURLSessionDataDelegate,NSURLSessionTaskDelegate,UITableViewDelegate>

@property (nonatomic, retain) UISegmentedControl *seg;
@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) CellForYaoWen *cellYaoWen;
@property (nonatomic, retain) CellForChinaJunqing *cellJunQing;

@end

@implementation ViewController

- (void)dealloc{
    
    _collection.dataSource = nil;
    _collection.delegate = nil;
    
    [_seg release];
    [_collection release];
    [_cellYaoWen release];
    [_cellJunQing release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self createNavigation];
    [self createSegment];
    [self createCollection];
    [self createKVOAction];
    [self createNot];
    
}


#pragma mark 夜间模式
- (void)viewDidAppear:(BOOL)animated {
    
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorWithColors([UIColor redColor], [UIColor colorWithRed:39 / 256.f green:39 / 256.f blue:39 / 256.f alpha:1]);
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    
    self.tabBarController.tabBar.dk_barTintColorPicker = DKColorWithColors([UIColor redColor], [UIColor colorWithRed:39 / 256.f green:39 / 256.f blue:39 / 256.f alpha:1]);
    
    self.collection.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
}

#pragma mark  创建NavigationController
- (void)createNavigation {
    
    self.navigationItem.title = @"中华军事";
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor],NSFontAttributeName : [UIFont fontWithName:@"AmericanTypewriter-Bold" size:22],}];
    
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    
    UIBarButtonItem *itemRight = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"WoDe"] style:UIBarButtonItemStylePlain target:self action:@selector(RightAction:)];
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    self.navigationItem.rightBarButtonItems = @[itemRight];
    
    [itemRight release];
}

- (void)RightAction:(UIBarButtonItem *)barbuttonItem {
    
    self.hidesBottomBarWhenPushed = YES;
    
    VCForMySelf *my = [[VCForMySelf alloc] init];
    
    [self.navigationController pushViewController:my animated:YES];
    
    [my release];
    
    self.hidesBottomBarWhenPushed = NO;
}

#pragma mark 创建头部 
- (void)createSegment {
    
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"要闻", @"中国军情"]];
    
    self.seg.frame = CGRectMake(0, 64, WIDTH, 30);
    
    [self.view addSubview:self.seg];
    
    // 更改SegmentedControl字体相关API
    self.seg.tintColor = [UIColor clearColor];
    
    // 设置放大 颜色
    NSDictionary *selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName:[UIColor redColor]};
    
    [self.seg setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];
    
    NSDictionary *unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName:[UIColor grayColor]};
    
    [self.seg setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    
    [_seg release];
    
    // 要闻为默认
    self.seg.selectedSegmentIndex = 1;
    
    [self.seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    
    self.seg.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
}

- (void)segAction:(UISegmentedControl *)seg {
  
    if (seg.selectedSegmentIndex == 0) {
        
        self.collection.contentOffset = CGPointMake(0, 0);
        
    } else {

        self.collection.contentOffset = CGPointMake(WIDTH, 0);
    }
}

#pragma mark 创建collectionView
- (void)createCollection {
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowlayout.itemSize = CGSizeMake(WIDTH, HEIGHT - 64 - 30 - 49);
    
    flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    flowlayout.minimumInteritemSpacing = 0;
    flowlayout.minimumLineSpacing = 0;
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64 + 30, WIDTH, HEIGHT - 64 - 30 - 49) collectionViewLayout:flowlayout];
    
    [self.view addSubview:self.collection];
    
    self.collection.dataSource = self;
    self.collection.delegate = self;
    
    self.collection.backgroundColor = [UIColor whiteColor];
    
    self.collection.pagingEnabled = YES;
    
    // 注册重用池
    [self.collection registerClass:[CellForYaoWen class] forCellWithReuseIdentifier:@"pool1"];
    
    [self.collection registerClass:[CellForChinaJunqing class] forCellWithReuseIdentifier:@"pool2"];
    
    [flowlayout release];
    [_collection release];
}

#pragma mark - collection datasource方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.item == 0) {
        
        self.cellYaoWen = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
        
        self.cellYaoWen.tableViewYaoWen.delegate = self;

        return self.cellYaoWen;
                
    } else {
        
        self.cellJunQing = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool2" forIndexPath:indexPath];
        
        self.cellJunQing.tableViewJunQing.delegate = self;
        
        return self.cellJunQing;
    }

}

#pragma mark - collectionViewDelegate方法 (KVO)
- (void)createKVOAction {
    
    [self.collection addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:@"ddd"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    CGFloat x = [[change objectForKey:@"new"] CGPointValue].x;
    
    for (int i = 0; i < 2; i++) {
        
        if (i * WIDTH == x) {
            
            self.seg.selectedSegmentIndex = i;
        }
    }
}

#pragma mark 点击Cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    VCForCell *vcCell = [[VCForCell alloc] init];
    NSArray *array = nil;
    
    self.hidesBottomBarWhenPushed = YES;
    
    if (self.cellYaoWen.tableViewYaoWen == tableView) {
       
        array = self.cellYaoWen.arrForYaowen;
        
        ModelForYaoWen *model = [array objectAtIndex:indexPath.row];

        // 接收数据
        vcCell.Location = model.NewsUrl;
        
    } else {
        
        array = self.cellJunQing.arrForJunQing;
        
        ModelForChinaJunQIng *model = [array objectAtIndex:indexPath.row];

        vcCell.Location = model.NewsUrl;
    }
    
    [self.navigationController pushViewController:vcCell animated:YES];

    self.hidesBottomBarWhenPushed = NO;
    
        [vcCell release];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100;
    
}

#pragma mark 通知推出问吧页面
-(void)createNot {
    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"questiontitle" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
        QuestionVC *speak = [[QuestionVC alloc] init];
        
        speak.model = [note.userInfo objectForKey:@"content"];
        
        [self presentViewController:speak animated:YES completion:nil];
        
        [speak release];
        
    }];

}













- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
